﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.WebUI.Models;
using SportsStore.Domain.Entities;

namespace SportsStore.WebUI.Controllers
{
    public class CartController : Controller
    {
        private IProductRepository repo;
        private IOrderProcessor orderProcess;

        public CartController(IProductRepository repository, IOrderProcessor proc)
        {
            repo = repository;
            orderProcess = proc;
        }

        //public ViewResult Index(string returnUrl)
        //{
        //    return View(new CartIndexViewModel { cart = GetCart(), ReturnUrl = returnUrl });
        //}

        public ViewResult Index(Cart c, string returnUrl)
        {
            return View(new CartIndexViewModel { cart = c, ReturnUrl = returnUrl });
        }


        //public RedirectToRouteResult AddToCart(int productId, string returnUrl)
        //{
        //    Product prod = repo.Products.FirstOrDefault(p => p.ProductID == productId);

        //    if (prod != null)
        //        GetCart().AddItem(prod, 1);

        //    return RedirectToAction("Index", new { returnUrl });
        //}

        public RedirectToRouteResult AddToCart(Cart c, int productId, string returnUrl)
        {
            Product prod = repo.Products.FirstOrDefault(p => p.ProductID == productId);

            if (prod != null)
                c.AddItem(prod, 1);

            return RedirectToAction("Index", new { returnUrl });
        }

        //public RedirectToRouteResult RemoveFromCart(int productId, string returnUrl)
        //{
        //    Product prod = repo.Products.FirstOrDefault(p => p.ProductID == productId);

        //    if (prod != null)
        //        GetCart().RemoveLine(prod);

        //    return RedirectToAction("Index", new { returnUrl });
        //}

        public RedirectToRouteResult RemoveFromCart(Cart c, int productId, string returnUrl)
        {
            Product prod = repo.Products.FirstOrDefault(p => p.ProductID == productId);

            if (prod != null)
                c.RemoveLine(prod);

            return RedirectToAction("Index", new { returnUrl });
        }


        public ViewResult Summary(Cart c)
        {
            return View(c);
        }

        //private Cart GetCart()
        //{
        //    Cart c = (Cart)Session["Cart"];
        //    if (c == null)
        //    {
        //        c = new Cart();
        //        Session["Cart"] = c;
        //    }

        //    return c;
        //}

        public ViewResult Checkout()
        {
            return View(new ShippingDetails());
        }

        [HttpPost]
        public ViewResult Checkout(Cart c, ShippingDetails shippingInfo)
        {
            if (c.Lines.Count() == 0)
            {
                ModelState.AddModelError("", "Sorry, Your cart is empty!");
            }

            if (ModelState.IsValid)
            {
                orderProcess.ProcessOrder(c, shippingInfo);
                c.Clear();
                return View("Completed");
            }
            else
            {
                return View(shippingInfo);
            }
        }
    }
}
